import { PROMISE, THUNK, ACTION } from "./action-type";
import { post } from "@/Utils/http";
import { handleActions as createReducer, createActions } from "redux-actions";
const initialState = {
  token: "",
  userName: "",
  userInfo: "",
  menus: [],
  status: false,
};

export function testSync(params) {
  return (dispatch, getState, { post }) => {
    dispatch({ type: PROMISE, payload: { status: "loading" } });
    return post("/app/mockv1/login", params)
      .then((res) => {
        dispatch({ type: PROMISE, payload: { ...res, status: "success" } });
      })
      .catch((err) => {
        dispatch({ type: PROMISE, payload: { status: "fail" } });
      });
  };
}

export function fun(params) {
  return (dispatch, _, { post }) => {
    new Promise((reslove, reject) => {
      setTimeout(() => {
        reslove({ aaa: "aaaaa" + params });
      }, 3000);
    }).then((res) => {
      console.log(res);
      dispatch({ type: "thunk", payload: res });
    });
  };
}
// export default (state = initialState, { type, payload }) => {
//   switch (type) {
//     case PROMISE:
//       console.log(payload);
//       return { ...state, ...payload };
//     case THUNK:
//       console.log(payload);
//       return { ...state, ...payload };
//     case ACTION:
//       console.log(payload);
//       return { ...state, ...payload };

//     default:
//       return state;
//   }
// };

export default createReducer(
  {
    [PROMISE](state, { type, payload }) {
      return { ...state, ...payload };
    },
    [THUNK](state, { type, payload }) {
      return { ...state, ...payload };
    },
    [ACTION](state, { type, payload }) {
      return { ...state, ...payload };
    },
  },
  initialState
);
